/*
 * MapModeToolbar.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.gui;

import org.fpdev.util.gui.ToolbarButton;
import org.fpdev.apps.admin.gui.map.MapPanel;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import org.fpdev.apps.admin.AdminClient;

public class MapModeToolbar extends JToolBar implements ActionListener {

  // other ACT_ vars defined in MapMainToolbar	
  public static final int ACT_MSELNODES = 101;
  public static final int ACT_MSELLINKS = 102;
  public static final int ACT_MSELCLINKS = 103;
  public static final int ACT_MNEWNODE = 104;
  public static final int ACT_MDELNODE = 105;
  public static final int ACT_MNEWLINK = 106;
  public static final int ACT_MDELLINK = 107;
  public static final int ACT_MSPLITLINK = 108;
  public static final int ACT_MMERGENODE = 109;
  public static final int ACT_MDRAGNODE = 110;
  public static final int ACT_MDRAGSHPPT = 111;
  public static final int ACT_MNEWSHPPT = 112;
  public static final int ACT_MDELSHPPT = 113;
  public static final int ACT_MSTITCHLINKS = 114;

  public static final int ACT_SNODE = 201;
  public static final int ACT_SRNNODE = 202;
  public static final int ACT_SLINK = 203;

  public static final int ACT_RPATH = 301;
  public static final int ACT_RDIVERTPATH = 302;
  public static final int ACT_RCLIPDIVERSION = 303;
  public static final int ACT_RADDTPOINT = 304;
  public static final int ACT_RDELTPOINT = 305;
  public static final int ACT_RADDSTOP = 306;
  public static final int ACT_RDELSTOP = 307;
  public static final int ACT_RCLIPPATHSTART = 308;
  public static final int ACT_RCLIPPATHEND = 309;

  public static final int ACT_AQNODE = 401; // query node
  public static final int ACT_AQLINK = 402; // query link
  public static final int ACT_ATRIPSTART = 403; // dijkstra start
  public static final int ACT_ATRIPEND = 404; // diskstra end
  public static final int ACT_ADEPFACS = 405;	// departing facilities
  public static final int ACT_AARRFACS = 406;	// arriving facilities
  
  private AdminClient ac_;
  
  private List<ToolbarButton> modeMapBtns_,  modeStationBtns_,  modeRouteBtns_,  modeAnalysisBtns_;
  private ButtonGroup anBtnGroup_,  rteBtnGroup_,  staBtnGroup_,  mapBtnGroup_; //btnGroup_;

  public MapModeToolbar(AdminClient ac, MapPanel mapPanel) { //, ButtonGroup btnGroup) {
    ac_ = ac;
    anBtnGroup_ = new ButtonGroup();
    rteBtnGroup_ = new ButtonGroup();
    staBtnGroup_ = new ButtonGroup();
    mapBtnGroup_ = new ButtonGroup();

    initComponents();
  }

  private void initComponents() {


    modeMapBtns_ = new ArrayList<ToolbarButton>(); //ArraySequence();
    modeStationBtns_ = new ArrayList<ToolbarButton>(); //new ArraySequence();
    modeRouteBtns_ = new ArrayList<ToolbarButton>(); //new ArraySequence();
    modeAnalysisBtns_ = new ArrayList<ToolbarButton>(); //new ArraySequence();

    String iconRoot = ac_.getEngine().getProperty("5pHome") + "img" + File.separator;
    
    addButton(new ImageIcon(iconRoot+"select_node.png"), "Select Nodes", ACGUI.Mode.MAP, ACT_MSELNODES, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"select_link_single.png"), "Select Links", ACGUI.Mode.MAP, ACT_MSELLINKS, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"select_link_path.png"), "Select Contiguous Links", ACGUI.Mode.MAP, ACT_MSELCLINKS, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"create_node.png"), "New Node", ACGUI.Mode.MAP, ACT_MNEWNODE, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"delete_node.png"), "Delete Node", ACGUI.Mode.MAP, ACT_MDELNODE, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"create_link.png"), "New Link", ACGUI.Mode.MAP, ACT_MNEWLINK, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"delete_link.png"), "Delete Link", ACGUI.Mode.MAP, ACT_MDELLINK, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"split_link.png"), "Split Link", ACGUI.Mode.MAP, ACT_MSPLITLINK, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"merge_node.png"), "Merge Node", ACGUI.Mode.MAP, ACT_MMERGENODE, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"move_node.png"), "Drag Node", ACGUI.Mode.MAP, ACT_MDRAGNODE, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"create_shppt.png"), "Add Shapepoint", ACGUI.Mode.MAP, ACT_MNEWSHPPT, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"delete_shppt.png"), "Delete Shapepoint", ACGUI.Mode.MAP, ACT_MDELSHPPT, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"move_shppt.png"), "Move Shapepoint", ACGUI.Mode.MAP, ACT_MDRAGSHPPT, mapBtnGroup_);
    addButton(new ImageIcon(iconRoot+"stitch_links.png"), "Stitch Links", ACGUI.Mode.MAP, ACT_MSTITCHLINKS, mapBtnGroup_);


    addButton(new ImageIcon(iconRoot+"select_sta_node.png"), "Select Station Nodes", ACGUI.Mode.STATION, ACT_SNODE, staBtnGroup_);
    addButton(new ImageIcon(iconRoot+"rename_sta_node.png"), "Rename Station Node", ACGUI.Mode.STATION, ACT_SRNNODE, staBtnGroup_);
    addButton(new ImageIcon(iconRoot+"select_sta_link.png"), "Select Station Links", ACGUI.Mode.STATION, ACT_SLINK, staBtnGroup_);

    addButton(new ImageIcon(iconRoot+"select_path.png"), "Select Path", ACGUI.Mode.ROUTE, ACT_RPATH, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"divert_path.png"), "Divert Path", ACGUI.Mode.ROUTE, ACT_RDIVERTPATH, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"clip_div.png"), "Clip Diversion", ACGUI.Mode.ROUTE, ACT_RCLIPDIVERSION, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"clip_start.png"), "Clip Path from Start", ACGUI.Mode.ROUTE, ACT_RCLIPPATHSTART, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"clip_end.png"), "Clip Path from End", ACGUI.Mode.ROUTE, ACT_RCLIPPATHEND, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"create_timept.png"), "Add Timepoint", ACGUI.Mode.ROUTE, ACT_RADDTPOINT, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"delete_timept.png"), "Delete Timepoint", ACGUI.Mode.ROUTE, ACT_RDELTPOINT, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"create_stop.png"), "Add Stop", ACGUI.Mode.ROUTE, ACT_RADDSTOP, rteBtnGroup_);
    addButton(new ImageIcon(iconRoot+"delete_stop.png"), "Delete Stop", ACGUI.Mode.ROUTE, ACT_RDELSTOP, rteBtnGroup_);

    addButton(new ImageIcon(iconRoot+"query_node.png"), "Query Stop", ACGUI.Mode.ANALYSIS, ACT_AQNODE, anBtnGroup_);
    addButton(new ImageIcon(iconRoot+"query_link.png"), "Query Link", ACGUI.Mode.ANALYSIS, ACT_AQLINK, anBtnGroup_);
    addButton(new ImageIcon(iconRoot+"trip_start.png"), "Trip Start", ACGUI.Mode.ANALYSIS, ACT_ATRIPSTART, anBtnGroup_);
    addButton(new ImageIcon(iconRoot+"trip_end.png"), "Trip End", ACGUI.Mode.ANALYSIS, ACT_ATRIPEND, anBtnGroup_);
    addButton(new ImageIcon(iconRoot+"dep_fac.png"), "Departing Facilities", ACGUI.Mode.ANALYSIS, ACT_ADEPFACS, anBtnGroup_);
    addButton(new ImageIcon(iconRoot+"arr_fac.png"), "Arriving Facilities", ACGUI.Mode.ANALYSIS, ACT_AARRFACS, anBtnGroup_);

    showModeButtons(modeAnalysisBtns_);
  }

  public ToolbarButton addButton(ImageIcon icon, String tip, ACGUI.Mode mode, int action, ButtonGroup bg) {
    return addButton(new ToolbarButton(icon, action), tip, mode, action, bg);
  }

  
  public ToolbarButton addButton(String key, String tip, ACGUI.Mode mode, int action, ButtonGroup bg) {
    return addButton(new ToolbarButton(key, action), tip, mode, action, bg);
  }

  public ToolbarButton addButton(ToolbarButton btn, String tip, ACGUI.Mode mode, int action, ButtonGroup bg) {
    btn.addActionListener(this);
    btn.setMargin(new Insets(0, 0, 0, 0));
    btn.setMinimumSize(new Dimension(24, 24));
    btn.setMaximumSize(new Dimension(34, 44));
    btn.setToolTipText(tip);
    if (btn instanceof ToolbarButton) {
      bg.add(btn);
    } ///btnGroup_.add(btn);
    switch (mode) {
      case MAP:
        modeMapBtns_.add(btn);
        break;
      case STATION:
        modeStationBtns_.add(btn);
        break;
      case ROUTE:
        modeRouteBtns_.add(btn);
        break;
      case ANALYSIS:
        modeAnalysisBtns_.add(btn);
        break;
      default:
        this.add(btn);
    }

    return btn;
  }

  
  public void showModeButtons(List<ToolbarButton> btnList) {
    Iterator<ToolbarButton> btns = btnList.iterator();
    while (btns.hasNext()) {
      ToolbarButton btn = btns.next();
      add(btn);
    }
  }

  public void hideModeButtons(List<ToolbarButton> btnList) {
    Iterator<ToolbarButton> btns = btnList.iterator();
    while (btns.hasNext()) {
      ToolbarButton btn = btns.next();
      remove(btn);
    }
  }

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() instanceof ToolbarButton) {
      //System.out.println("New click action");
      ac_.getGUI().setToolbarAction(((ToolbarButton) e.getSource()).getClickAction());
    }
  }

  public void newMode(ACGUI.Mode oldmode, ACGUI.Mode mode) {
    System.out.println("mode: " + oldmode + " to " + mode);
    switch (oldmode) {
      case MAP:
        hideModeButtons(modeMapBtns_);
        break;
      case STATION:
        hideModeButtons(modeStationBtns_);
        break;
      case ROUTE:
        hideModeButtons(modeRouteBtns_);
        break;
      case ANALYSIS:
        hideModeButtons(modeAnalysisBtns_);
        break;
    }

    switch (mode) {
      case MAP:
        showModeButtons(modeMapBtns_);
        break;
      case STATION:
        showModeButtons(modeStationBtns_);
        break;
      case ROUTE:
        showModeButtons(modeRouteBtns_);
        break;
      case ANALYSIS:
        showModeButtons(modeAnalysisBtns_);
        break;
    }
    repaint();
  }

  public void clearSelected() {
    anBtnGroup_.clearSelection();
    rteBtnGroup_.clearSelection();
    staBtnGroup_.clearSelection();
    mapBtnGroup_.clearSelection();
  }
}






















